package org.onvivals.app.weixin.controller;

import cn.hutool.core.util.EscapeUtil;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import com.github.binarywang.wxpay.bean.notify.WxPayOrderNotifyResult;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.onvivals.common.enums.BillDetailEnum;
import org.onvivals.common.enums.OrderInfoEnum;
import org.onvivals.module.ums.entity.UmsUserRecharge;
import org.onvivals.module.ums.service.IUmsUserRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Tag(name = "微信模块")
@RequestMapping("/wechat")
public class WechatController {

	private final WxPayService wxPayService;
	private final IUmsUserRechargeService pmsMemberRechargeService;

	/**
	 * 微信支付/充值回调
	 */
	@PostMapping("/notify")
	@Operation(summary = "微信支付充值回调")
	public String renotify(@RequestBody String xmlData) {
		xmlData = EscapeUtil.unescapeXml(xmlData);
		log.info("xmlData:{}", xmlData);
		try {
			WxPayOrderNotifyResult notifyResult = wxPayService.parseOrderNotifyResult(xmlData);
			String orderId = notifyResult.getOutTradeNo();
			String attach = notifyResult.getAttach();
			log.info("orderId:{}", orderId);
			log.info("attach:{}", attach);
			if (BillDetailEnum.TYPE_1.getValue().equals(attach)) {
				//处理充值
				UmsUserRecharge userRecharge = pmsMemberRechargeService.getInfoByOrderId(orderId);
				if (userRecharge == null) {
					return WxPayNotifyResponse.success("处理成功!");
				}
				if (OrderInfoEnum.PAY_STATUS_1.getValue().equals(userRecharge.getPaid())) {
					return WxPayNotifyResponse.success("处理成功!");
				}
				pmsMemberRechargeService.updateRecharge(userRecharge);
			}
			if (BillDetailEnum.TYPE_13.getValue().equals(attach)) {
				//处理充值
				UmsUserRecharge userRecharge = pmsMemberRechargeService.getInfoByOrderId(orderId);
				if (userRecharge == null) {
					return WxPayNotifyResponse.success("处理成功!");
				}
				if (OrderInfoEnum.PAY_STATUS_1.getValue().equals(userRecharge.getPaid())) {
					return WxPayNotifyResponse.success("处理成功!");
				}
				pmsMemberRechargeService.updateRecharge(userRecharge);
			}
			if (BillDetailEnum.TYPE_14.getValue().equals(attach)) {
				//处理充值
				UmsUserRecharge userRecharge = pmsMemberRechargeService.getInfoByOrderId(orderId);
				if (userRecharge == null) {
					return WxPayNotifyResponse.success("处理成功!");
				}
				if (OrderInfoEnum.PAY_STATUS_1.getValue().equals(userRecharge.getPaid())) {
					return WxPayNotifyResponse.success("处理成功!");
				}
				pmsMemberRechargeService.updateRecharge(userRecharge);
			}
			return WxPayNotifyResponse.success("处理成功!");
		} catch (WxPayException e) {
			log.error(e.getMessage());
			return WxPayNotifyResponse.fail(e.getMessage());
		}
	}
}
